package yxy.game.pm2.command.pack;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.currency.CurrencyType;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.PackageMessage;
import yxy.game.pm2.module.PackageHandle;

/** 背包 查询某物品数量 */
@MessageType(proto = Command.PackageItemNumber, classtype = PackageMessage.PackageItemNumberRequest.class)
public class CMD_PackageItemNumber extends Command<PackageMessage.PackageItemNumberRequest> {

	@Override
	public void execute(MessageSource messageSource, PackageMessage.PackageItemNumberRequest request) {
		String playerCode = messageSource.getTag();
		int type = request.getType();
		int dataId = request.getDataId();

		PackageMessage.PackageItemNumberResponse.Builder response = PackageMessage.PackageItemNumberResponse.newBuilder();

		CurrencyType currencyType = CurrencyType.forNumber(type);
		if (currencyType == null) {
			send(messageSource, response, Result.ParamError);
			return;
		}

		response.setItem(new PackageHandle().getItemByDataId(playerCode, currencyType, dataId));
		send(messageSource, response, Result.Success);
	}

	private void send(MessageSource messageSource, PackageMessage.PackageItemNumberResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}